package org.hackystat.sdsa.analyzer.model.activity.refactoring;

import java.util.Map;
import java.util.logging.Logger;

import jess.Fact;
import jess.JessException;
import jess.RU;
import jess.Rete;
import jess.Value;

import org.hackystat.sdsa.analyzer.control.SensorDataUtil;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;

/**
 * Defines unary refactoring action.
 * 
 * @author Hongbing Kou
 */
public final class UnaryRefactorActivity extends RefactorActivity {
  /** Refactor subject name. */
  private String subjectName;
  /** Unit name. */
  public static final String UNIT_NAME = "unit-name";

  /**
   * Empty constructor.
   * 
   * @param op RefactorOperator.
   * @param st SubjectType.
   */
  public UnaryRefactorActivity (RefactorOperator op, RefactorSubjectType st) {
    super(op, st);
  }
  
  /**
   * Constructs an unary refactor action.
   * 
   * @param sensorData Sensor data.
   * @param logger Logger.
   */
  public UnaryRefactorActivity(SensorData sensorData, Logger logger) {
    super(sensorData, logger);
    
    Map<String, String> propMap = SensorDataUtil.propMap(sensorData);
    this.subjectName = propMap.get(UNIT_NAME);
  }

  /**
   * Sets refactoring subject name.
   * 
   * @param subjectName Refactoring subject name.
   */
  public void setSubjectName(String subjectName) {
    this.subjectName = subjectName;
  }

  /**
   * Gets subject name.
   * 
   * @return Subject name.
   */
  public String getSubjectName() {
    return this.subjectName;
  }  

  /**
   * Makes Jess facts in the given Jess rete engine.
   * 
   * @param index Index of action in episode. 
   * @param engine Jess rete engine.
   * @throws JessException If error while constructing jess action.
   * @return Jess fact of this action.
   */
  @Override public Fact assertActivity(int index, Rete engine) throws JessException  {
    Fact f = new Fact("UnaryRefactorActivity", engine);
    f.setSlotValue(INDEX_SLOT, new Value(index, RU.INTEGER));
    String file = super.extractFileName(super.getResourceUri());
    f.setSlotValue(FILE_SLOT, new Value(file, RU.STRING));
    f.setSlotValue("operation", new Value(this.getOperator().getName(), RU.STRING));
    f.setSlotValue("type", new Value(this.getSubjectType().getName(), RU.STRING));
    f.setSlotValue("data", new Value(this.getSubjectName(), RU.STRING));
    
    Fact assertedFact = engine.assertFact(f);
    return assertedFact;
  }  
  
  
  /**
   * Gets refactor action string.
   * 
   * @return Refactor action string.
   */
  @Override public String toString() {
    return super.toString() + " {" + this.subjectName + "}";
  }

  /** 
   * Get the representation of this activity. 
   * 
   * @return String representation of this activity.
   */
  @Override public String getRepresentation() {
    return this.subjectName;
  }

}
